#include "SnImage.h"
#include "base/SnImage.h"
#include <memory>
#include <stdint.h>
#include <vcruntime_string.h>
#include "iostream"
#define STB_IMAGE_IMPLEMENTATION
#include "utils/stb_image.h"

namespace Sivin {
  SnImage::SnImage(int w, int h, int channels, const void *data)
      : mWidth(w), mHeight(h), mChannels(channels) {
    int size = w * h * channels;
    if (size != 0 && data != nullptr) {
      uint8_t *pixelData = new uint8_t[size];
      if (pixelData) {
        memcpy(pixelData, data, size);
        mData = std::unique_ptr<uint8_t>(pixelData);
      }
    }
  }

  SnImage::~SnImage() {}

  std::unique_ptr<SnImage> SnImage::loadImage(std::string path) {
    SnImage *img = nullptr;
    //stb_img读入的图片默认是反的
    stbi_set_flip_vertically_on_load(true);
    int width, height, channels;
    unsigned char *data = stbi_load(path.c_str(), &width, &height, &channels, 0);
    if (data) {
      img = new SnImage(width, height, channels, data);
      stbi_image_free(data);
    } else {
      std::cout << "load imge failed.\n";
    }
    return std::unique_ptr<SnImage>(img);
  }

}// namespace Sivin